클라리언트와 서버가 데이터를 주고받는 규약(프로토콜)
특징 :
구성 :
Request
시작 라인(Request Line) :
<HTTP 메서드><요청 대상 경로(URL)><HTTP 버전>
GET /users/1 HTTP/1.1헤더(Header) :
key: value 형태로 구성Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/html
Content-Type: application/json
Content-Length: 123
Authorization: Bearer <토큰>
바디(Body) :
{
"username": "hyoensu",
"password": "1234"
}
HTTP 요청 전체 예시
POST /login HTTP/1.1 <-- 시작줄 Host: www.example.com <-- 헤더 시작 Content-Type: application/json Content-Length: 48 Authorization: Bearer abc123 { <-- 빈 줄 이후부터 바디 시작 "username": "hyoensu", "password": "1234" }
Response
상태줄(Status Line) :
<HTTP 버전> <상태 코드> <상태 메시지>
HTTP/1.1 200 OK
대표 상태 코드 :
헤더(Header) : 응답에 대한 정보 제공
Content-Type: application/json Content-Length: 178 Set-Cookie: sessionId=abc123; HttpOnly Cache-Control: no-cache
장점/단점 :
HTTP 메서드
GET
/users/1POST
/users + {name:"홍길동"}PUT
/users/1 + {name:"이순신}PATCH
/users/1 + {age:30}DELETE
/users/1Safe / Idempotent
상태코드
1xx (정보, Informational) :
100 Continue : 요청의 일부만 받고 계속 진행101 Switching Protocols : 프로토콜 전환 (ex : HTTP -> WebSocket)2xx (성공, Success) :
200 OK : 성공(GET,PUT,DELETE 등)201 Created : 리소스가 성공적으로 생성됨 (POST 성공시 자주 사용)204 No Content : 성공했지만 응답 본문 없음 (DELETE 성공 시 주로 사용)3xx(리다이렉션, Redirection) :
301 Moved Permanently : 요청한 자원이 영구적으로 다른 위치로 이동302 Found : 일시적 리다이렉션304 Not Modified : 캐시 사용 (변경 없음)4xx(클라이언트 오류, Client Error) :
400 Bad Reqeust : 잘못된 요청(파라미터 문제 등)401 Unauthorized : 인증 필요(로그인 필요)403 Forbidden : 권한 없음(인증은 했지만 접근 불가)404 Not Found : 요청한 자원 없음409 Conflict : 리소스 충돌5xx(서버 오류, Server Error) :
500 Internal Server Error : 일반적 서버 오류502 Bad Gateway : 게이트웨이/프록시 서버가 잘못된 응답 받음503 Server Unavailable : 서버 과부하/점검 중504 Gateway Timeout : 게이트웨이/프록시가 응답을 기다리다 시간 초과HTTP + TLS/SSL로 암호화된 통신
구성
특징 :
암호화 (Encryption)
무결성 (Integrity)
인증 (Authentication)
동작 방식 (Handshake)
클라이언트 -> 서버
서버 -> 클라이언트
클라이언트 인증서 검증
키 교환(대칭키 공유)
대칭키 암호화 통신 시작
HTTP를 기반으로 **자원(Resource)**을 URI로 표현하고, CRUD를 HTTP 메서드로 처리하는 아키텍쳐 스타일
HTTP를 이용하는 규칙
특징 :
예시 :
/users/1 -> 사용자 조회/users -> 사용자 생성/users/1 -> 사용자 전체 수정/users/1 -> 사용자 삭제JSON
키-값 쌍으로 데이터를 표현하는 텍스트 포맷
특징:
예시
{ "id": 1, "name": "홍길동", "email": "hong@test.com" }
@RequestBody, @ResponseBody -> JSON 직렬화/역직렬화
@RequestBody
@PostMapping("/users") public ResponseEntity<String> createUser(@RequestBody User user) { // 클라이언트가 보낸 JSON → User 객체로 변환 userService.save(user); return ResponseEntity.ok("User created"); }
@ResponseBody
@GetMapping("/users/{id}") @ResponseBody public User getUser(@PathVariable Long id) { // DB에서 조회한 User 객체를 JSON으로 반환 return userService.findById(id); }
REST API
RESTful API
@RestController @RequestMapping("/users") public class UserController { @GetMapping("/{id}") public User getUser(@PathVariable int id) { return userService.getUser(id); } @PostMapping public User createUser(@RequestBody User user) { return userService.createUser(user); } @PutMapping("/{id}") public User updateUser(@PathVariable int id, @RequestBody User user) { return userService.updateUser(id, user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable int id) { userService.deleteUser(id); } }
자원 중심 설계
HTTP 메서드 적절히 사용
상태 비저장
표준 상태 코드 활용
표현 사용
HTTP 요청 처리 + 정적 컨텐츠 제공
예시 : Apache, Nginx
역할
동적 컨텐츠 처리 + 비즈니스 로직 수행
예시 : Tomcat, Jetty, JBoss
역할
요청 흐름
클라이언트 요청 → 웹서버 → WAS → 컨트롤러 → 서비스 → DAO/Repository → DB → 응답 JSON/HTML
톰캣(Tomcat) 구조
Connector :
Engine :
Host :
Context :
WEB-INF/web.xml)Servlet :
Session 관리 : HttpSession 제공, 상태 관리
프론트엔드 연계
REST API 호출 : fetch, Axios, jQuery, AJAX
JSON 데이터 송수신
fetch('/users/1') .then(res => res.json()) .then(data => console.log(data));